<?php 
if(isset($_REQUEST['fuseaction_back'])) {
	$fuseaction_back=$_REQUEST['fuseaction_back'];
}else{
	$fuseaction_back = "";
}
if(isset($_REQUEST['hdn_worktype'])) {
	$v_worktype = $_REQUEST['hdn_worktype'];
}else{
	$v_worktype = "";
}
//Luu giu trang thai loc
$v_receive_date_filter = "";
if(isset($_REQUEST['hdn_received_date_filter'])){
	$v_receive_date_filter = $_REQUEST['hdn_received_date_filter'];
} 
$v_recordtype_filter = $_SESSION['onegate_current_recordtype'];
if(isset($_REQUEST['hdn_recordtype_filter'])){
	$v_recordtype_filter = $_REQUEST['hdn_recordtype_filter'];
}  
//Begin//Xay dung chuoi XML chua gi tri cua 
$v_list_xml_tag = "";
if(isset($_REQUEST['hdn_list_xml_tag'])){
	$v_list_xml_tag = $_REQUEST['hdn_list_xml_tag'];
}
$v_list_xml_value = "";
if(isset($_REQUEST['hdn_list_xml_value'])){
	$v_list_xml_value = $_REQUEST['hdn_list_xml_value'];
}
$v_value_status_show_hide = "";
//Trang thai thieu HS hay khong? 0: Ung voi thieu HS; 1: Ung voi du HS
$v_status_minus_record = 0;
$v_tag = 0;
$strXML = '<?xml version="1.0" encoding="UTF-8"?><root><data_list>';
for ($i=0;$i<_list_get_len($v_list_xml_tag,_CONST_SUB_LIST_DELIMITOR);$i++){
	$strXML = $strXML ."<"._list_get_at($v_list_xml_tag,$i,_CONST_SUB_LIST_DELIMITOR).">";
	
	if(_list_get_at($v_list_xml_tag,$i,_CONST_SUB_LIST_DELIMITOR) == "status_record"){
		$v_value_status_show_hide = trim(_replace_XML_bad_char(_list_get_at($v_list_xml_value,$i,_CONST_SUB_LIST_DELIMITOR)));
		$v_tag = 1;
	}
	if((_list_get_at($v_list_xml_tag,$i,_CONST_SUB_LIST_DELIMITOR) == "minus_documnent") && ($v_value_status_show_hide == "false")){
		$strXML = $strXML ."";
		$v_status_minus_record = 1;
	}else{
		$strXML = $strXML .trim(_replace_XML_bad_char(_list_get_at($v_list_xml_value,$i,_CONST_SUB_LIST_DELIMITOR)));
	}		
	$strXML = $strXML ."</"._list_get_at($v_list_xml_tag,$i,_CONST_SUB_LIST_DELIMITOR).">";
}
$strXML = $strXML . "</data_list></root>";
//Kiem tra trong truong hop co ton tai the khong?
if($v_tag <= 0){
	$v_status_minus_record = 1;
}
//End//Lay danh sach cac the XML va gia tri tuong ung voi tung the do
$v_record_id = 0;
if(isset($_REQUEST['hdn_record_id'])){
	$v_record_id = intval($_REQUEST['hdn_record_id']);
}
$v_code_record = "";
if(isset($_REQUEST['txt_code_record'])){
	$v_code_record = _replace_bad_char($_REQUEST['txt_code_record']);
}
//ngay tiep nhan
$v_received_date = "";
if(isset($_REQUEST['txt_received_date'])){
	$v_received_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['txt_received_date']);
}
//ngay hen tra ket qua tra ket qua
$v_appointed_date = "";
if(isset($_REQUEST['txt_appointed_date'])){
	$v_appointed_date = _ddmmyyyy_to_yyyymmdd($_REQUEST['txt_appointed_date']);
}

if($v_value_status_show_hide == "true"){
	$v_appointed_date = "";
}

$v_status = $arr_record_status[0][0];
$v_recordtype = $_REQUEST['hdn_recordtype_filter'];
$v_receiver_id = $_SESSION['staff_id'];
$v_image_id = 0;
$v_deleted_exist_file_id_list=$_REQUEST['hdn_deleted_exist_file_id_list']; //Danh sach cac id file da ton tai bi xoa
$v_deleted_new_file_id_list=$_REQUEST['hdn_deleted_new_file_id_list'];  //Danh sach cac id file them moi da bi xoa
$v_new_file_id = 0;
$v_new_file_id_list="";
if(_is_sqlserver()){
	for($i=0; $i<_CONST_MAX_NUMBER_ATTACH_FILE; $i++){
		$v_form_field = 'file_attach'.$i;		
		if (isset($_FILES[$v_form_field]['tmp_name'])){
			// Neu file nay khong thuoc danh sach file da bi xoa			
			if ($v_deleted_new_file_id_list=="" Or _list_have_element($v_deleted_new_file_id_list, $i, ",")==false){
				$v_filename = _replace_bad_char(trim($_FILES[$v_form_field]['name']));
				$v_tmp_filename = $_FILES[$v_form_field]['tmp_name'];
				$v_file_id = 0;
				if ($v_tmp_filename!="" and !is_null($v_tmp_filename)){
					_copy_file($v_tmp_filename, _CONST_LEVEL2_UPLOAD_IMAGE_PATH_FROM_CURRENT.$v_filename);
					if ($v_new_file_id_list ==""){
						$v_new_file_id_list = _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL2_UPLOAD_IMAGE_PATH_FROM_CURRENT.$v_filename);
					}else{			
						$v_new_file_id_list = $v_new_file_id_list . ",". _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL2_UPLOAD_IMAGE_PATH_FROM_CURRENT.$v_filename);
					}	 
				}	
			}	
		}	
	}
	$received_record_xml_date_id = _save_XML_to_database('T_TEMP_FILE', 'PK_TEMP_FILE', 'C_TEXT', $strXML);
	//Cap nhat noi dung cau hoi
	$sql = "Exec Onegate_RecordUpdate " ;
	$sql = $sql . " " .  $v_record_id ;
	$sql = $sql . ",'" . $v_status . "'"  ;
	$sql = $sql . ",'" . $v_recordtype . "'"  ;
	$sql = $sql . "," .  $v_receiver_id ;
	$sql = $sql . ",'" . $v_code_record . "'" ;
	$sql = $sql . ",'" . $v_received_date . "'" ;
	$sql = $sql . ",'" . $v_appointed_date . "'" ;
	$sql = $sql . "," .  $received_record_xml_date_id ;
	$sql = $sql . ",'" . $v_worktype . "'" ;
	$sql = $sql . "," . $_ISA_AUTOMATIC_UPDATE_WORK_LIST;
	//$sql = $sql . "," . $v_image_id;
	$sql = $sql . "," . $v_status_minus_record;	
	$sql = $sql . ",'" . $v_new_file_id_list . "'" ;
	$sql = $sql . ",'" . $v_deleted_exist_file_id_list . "'" ;
	//echo $sql;exit;
	$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
	$rs = $ado_conn->GetRow($sql); 
	$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	if($v_record_id <= 0){
		$v_record_id = $rs['NEW_ID'];
	}
}
sleep(0);
if (!is_null($v_error) && $v_error<>""){?>
	<script>
		alert("<?php echo $v_error; ?>");
		if (_MODAL_DIALOG_MODE==1){
			window.location = "<?php echo $v_goto_url; ?>";
		}else{
			window.history.back();
		}	
	</script><?php
	exit;
}
?>
<form action="index.php" method="post" name="f_back">
	<input type="hidden" name="fuseaction" value="<?php echo $fuseaction_back; ?>">
	<input type="hidden" name="fuseaction_back" value="<?php echo $fuseaction_back; ?>">
	<input type="hidden" name="hdn_record_id" value="<?php echo $v_record_id;?>">
	<input type="hidden" name="hdn_recordtype_filter" value="<?php echo $v_recordtype_filter; ?>">
	<input type="hidden" name="hdn_received_date_filter" value="<?php echo $v_receive_date_filter; ?>">
	<input type="hidden" name="hdn_current_item_id" value="<?php echo $v_record_id;?>">
	<input type="hidden" name="hdn_receive_type" value="<?php echo $v_receive_type; ?>">
</form>
<Script language="javascript">
	document.forms(0).submit();
</Script>